package com.example.concurrentprogramming8016.reetrantlock;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author liyanhao
 * @Date 2023-01-05 18:32
 * 可重入锁
 */
@Slf4j
public class ReentrantLockDemo2 {
    private static ReentrantLock lock = new ReentrantLock();

    public static void main(String[] args) {
        for (int i = 0; i < 1000; i++) {
            new Thread(() -> {
                method1();
            }).start();
        }
    }

    private static void method1() {
        lock.lock();
        try {
            log.debug("execute method1" + Thread.currentThread().getName());
            method2();
        } finally {
            lock.unlock();
        }
    }

    private static void method2() {
        lock.lock();
        try {
            log.debug("execute method2" + Thread.currentThread().getName());
        } finally {
            lock.unlock();
        }
    }
}
